<?php

namespace App\Admin\Extensions\ExcelExport;

use App\Models\Area;
use App\Models\Scene;
use Encore\Admin\Grid\Exporters\AbstractExporter;
use Maatwebsite\Excel\Facades\Excel;

class FlowExpoter extends AbstractExporter
{
    public function export()
    {
        ini_set('memory_limit','500M');
        set_time_limit(0);//设置超时限制为0分钟
        Excel::create('景区客流', function($excel) {
            $excel->sheet('客流', function($sheet) {
                // 这段逻辑是从表格数据中取出需要导出的字段
                $rows = collect($this->getData())->map(function ($item) {
                    $item['scene_id'] = $item['scene_id']!=null?Scene::find($item['scene_id'])->title:'';
                    $item['time'] = $item['time']?config('options.flow_hour')[$item['time']]:'';
                    return array_only($item, ['id','scene_id','date', 'time','num','in','out']);
                });
                $sheet->prependRow(['ID', '景区','日期', '时间','人数','流进','流出']);
                $sheet->row(1, function($row) {
                    $row->setAlignment('center');
                    $row->setFont(array(
                        'size'       => '12',
                        'bold'       =>  true
                    ));
                });
                $sheet->rows($rows);
            });
        })->export('xls');
    }
}